package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author tangjian
 * @Date 2021/10/31 10:17
 * @description
 */

@Service
public class HomeService implements IHomeService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;



    @Override
    public HomePageResponse homepage() {
        // TODO: 2021/10/31 缓存处理
        HomePageResponse response = new HomePageResponse();

        try {

            List<Panel> panels = panelMapper.selectAllIdHomePage();
            Set<PanelDto> panelDtos = new HashSet<>();

            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());

                panel.setPanelContentItems(panelContentItems);
                PanelDto panelDto = contentConverter.panen2Dto(panel);
                panelDtos.add(panelDto);
            }

            response.setPanelContentItemDTOs(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return response;
    }
}
